package com.capgemini.fs.app.ekms.persistence.dao.implementations;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.capgemini.fs.app.ekms.model.Topic;
import com.capgemini.fs.app.ekms.persistence.dao.TopicDAO;
import com.capgemini.fs.app.ekms.persistence.util.QueryHelper;

public class TopicDAOImpl extends BaseDAOHibernateImpl<Topic> implements
		TopicDAO {

	public void addTopic(Topic topic) {
		save(topic);
	}
	@SuppressWarnings("unchecked")
	public List<Topic> queryTopics(final Integer forumId,final String subject, final Integer status) {

		List topicList = (List<Topic>) getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Criteria c = session.createCriteria(Topic.class);
						if(!QueryHelper.isNullOrEmpty(forumId)){
							c.add(Restrictions.eq("forum.id", forumId));
						}
						if (!QueryHelper.isNullOrEmpty(subject)) {
							c.add(Restrictions.like("subject", "%" + subject
									+ "%"));
						}
						if (!QueryHelper.isNullOrEmpty(status)) {
							c.add(Restrictions.eq("status", status));
						}
						c.setCacheable(true);
						return c.list();
					}

				});

		return topicList;

	}
	
}
